<template>
  <div>
    <swiper ref="mySwiper" :options="swiperOptions">
      <swiper-slide v-for="item in banner" :key="item.title">
        <img :src="item.image" alt=""  width="100%"/>
      </swiper-slide>
      <div class="swiper-pagination" slot="pagination"></div>
    </swiper>
    <!-- 轮播图片按钮 -->
    <div>
      <ul class="list">
        <li
          v-for="(item,index) in banner"
          :key="item.title"
          :class="{ active: index == num }"
          @click="changeSwiper(index)"
        >
          {{ item.title }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import api from "../../api/index";
export default {
  data() {
    let that = this;
    return {
      num: 0,
      arr: [1, 2, 3, 4, 5],
      banner: [],
      swiperOptions: {
        pagination: {
          el: ".swiper-pagination",
        },
        autoplay: {
          delay: 3000,
          stopOnLastSlide: false,
          disableOnInteraction: false,
        },
        loop: true,
        on: {
          slideChangeTransitionStart: function () {
            // console.log("activeIndex---" + this.activeIndex);
            //this.slides.eq(this.activeIndex).find('.ani').removeClass('ani'); 动画只展现一次，去除ani类名
            if (this.activeIndex == that.arr.length + 1) {
              that.num = 0;
              //   console.log(that.num);
            } else {
              that.num = this.activeIndex - 1;
              //    console.log(that.num);
            }

            // console.log("num---" + that.num);
          },
        },
      },
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.$swiper;
    },
  },
  mounted() {
    api.getSwiperBanner().then((res) => {
      console.log(res.data);
      this.banner = res.data.result;
      console.log(this.banner);
    });
  },
  methods: {
    changeSwiper(index) {
      //选中高亮
      this.num = index;
      //让swiper切换
      this.swiper.slideTo(index + 1, 1000, false);
    },
  },
};
</script>

<style lang="less" scoped>
.list {
  display: flex;
  li {
    float: left;
    flex: 1;
    padding: 0.1rem;
    font-size: 0.24rem;
    line-height: 0.32rem;
    margin: 0.1rem;
    background: #eee;
    color: #666;
    text-align: center;
    display: flex;
    align-items: center;
  }
  .active {
    background: cornflowerblue;
    color: #000;
  }
}
</style>